package groovy
import com.aliyun.dingtalkworkflow_1_0.Client
import com.aliyun.dingtalkworkflow_1_0.models.QueryIntegratedTodoTaskHeaders
import com.aliyun.dingtalkworkflow_1_0.models.QueryIntegratedTodoTaskRequest
import com.aliyun.dingtalkworkflow_1_0.models.QueryIntegratedTodoTaskResponseBody
import com.aliyun.teaopenapi.models.Config
import com.aliyun.teautil.models.RuntimeOptions
static Client createClient() {
    Config config = new Config();
    config.protocol = "https";
    config.regionId = "central";
    return new Client(config);
}
static List<String> getDingUserTaskList() {
    Client client = createClient();
    QueryIntegratedTodoTaskHeaders queryIntegratedTodoTaskHeaders = new QueryIntegratedTodoTaskHeaders();
    queryIntegratedTodoTaskHeaders.xAcsDingtalkAccessToken = GetDingTokenScript.getTokenFromRedis();
    QueryIntegratedTodoTaskRequest queryIntegratedTodoTaskRequest = new QueryIntegratedTodoTaskRequest()
            .setUserId("06400301281217267")
            .setPageSize(40)
            .setPageNumber(1)

    def res=client.queryIntegratedTodoTaskWithOptions(queryIntegratedTodoTaskRequest, queryIntegratedTodoTaskHeaders, new RuntimeOptions());
    List<String> taskList=[]
    res.body.result.list.forEach {taskList.add(it.activityId)}
    taskList
}
static List< QueryIntegratedTodoTaskResponseBody.QueryIntegratedTodoTaskResponseBodyResultList> getAllDingUserTaskList() {
    Client client = createClient();
    QueryIntegratedTodoTaskHeaders queryIntegratedTodoTaskHeaders = new QueryIntegratedTodoTaskHeaders();
    queryIntegratedTodoTaskHeaders.xAcsDingtalkAccessToken = GetDingTokenScript.getTokenFromRedis();


    def taskList = []
    int pageNumber = 1
    boolean hasMore = true

    while (hasMore) {
        QueryIntegratedTodoTaskRequest queryIntegratedTodoTaskRequest = new QueryIntegratedTodoTaskRequest()
                .setUserId("06400301281217267")
                .setPageSize(40)
                .setPageNumber(pageNumber)

        def res = client.queryIntegratedTodoTaskWithOptions(queryIntegratedTodoTaskRequest, queryIntegratedTodoTaskHeaders, new RuntimeOptions());

        if (res.body.result.list) {
            res.body.result.list.forEach {
                taskList.add(it)
            }
        }

        // 根据API响应检查是否还有更多页面
        hasMore = res.body.result.hasMore ?: false
        pageNumber++
    }

    taskList
}

getDingUserTaskList().forEach {println(it)}